
package linuxbox.bbs.servlet;

import javax.servlet.http.*;

import java.io.*;

import linuxbox.BoxStatic;
import linuxbox.bbs.logic.*;
import linuxbox.common.*;
import linuxbox.exception.*;

public class BbsDelAction extends Action {
  protected static String successJSP = null;

    public void doAction(HttpSession aSession, BoxSession aBoxSession,
    HttpServletRequest req, HttpServletResponse res) throws Exception {
    String sCatID = req.getParameter("CAT_ID");
    if (sCatID == null || sCatID.equals(""))
      throw new LinuxboxException(ExceptionHelper.BBS, "B000001");

    String sBackUrl = req.getParameter("BACK_URL");
    if (sBackUrl == null || sBackUrl.equals(""))
      sBackUrl = BoxStatic.servletDir+"BbsContentAction" + "?CAT_ID=" + sCatID;
    else
      sBackUrl = sBackUrl + "?CAT_ID=" + sCatID;

    String sMsgID = req.getParameter("MSG_ID");
    long lMsgID = 0;
    if (sMsgID == null || sMsgID.equals(""))
      throw new LinuxboxException(ExceptionHelper.BBS, "B000002");
    else
      lMsgID = Long.parseLong(sMsgID);

    BbsManager bm = BbsManager.getInstance();

    res.setContentType("text/html; charset=gb2312");
    res.setHeader("Pragma", "no-cache");
    res.setHeader("Cache-Control", "no-cache");
    res.setDateHeader("Expires", 0);

    PrintWriter out = new PrintWriter(res.getOutputStream());

    int ret = bm.canDelMsg(aBoxSession, lMsgID);
    if (ret == BbsManager.CAN_DEL) {
      bm.delBbsMsg(lMsgID);
      out.println("<html><head><META HTTP-EQUIV=\"Refresh\"" +
        "CONTENT=\"0; URL=" + sBackUrl + "\"></head></html>");
    } else if (ret == BbsManager.NO_RIGHT) {
        throw new LinuxboxException(ExceptionHelper.BBS, "B000003");
    } else if (ret == BbsManager.HAS_CHILD) {
        throw new LinuxboxException(ExceptionHelper.BBS, "B000004");
    }

    out.flush();
    out.close();
  }

  public void setSuccessJSP(String str) {
    successJSP = str;
  }
}
